var PROJECT_NAME="/OASystem";


//设置按钮列的元素样式
function setCell(edit,ok,del,show){
	$('.editcls').linkbutton({text:'',plain:false,iconCls:'icon-edit'});
	$('.okcls').linkbutton({text:'',plain:false,iconCls:'icon-ok'});
	$('.delcls').linkbutton({text:'',plain:false,iconCls:'icon-cancel'});
	$('.nocls').linkbutton({text:'',plain:false,iconCls:'icon-no'});
	$('.showcls').linkbutton({text:'',plain:false,iconCls:'icon-search'});
	setToolTip(edit,ok,del,show);
}
//设置提示文本
function setToolTip(edit,ok,del,show){
	$('.editcls').tooltip({    
		position: 'bottom',
		trackMouse:'true',
		content: '<span>'+edit+'</span>'    
	});
	$('.okcls').tooltip({    
		position: 'bottom',
		trackMouse:'true',
		content: '<span>'+ok+'</span>'    
	});
	$('.delcls,.nocls').tooltip({    
		position: 'bottom',
		trackMouse:'true',
		content: '<span>'+del+'</span>'    
	});
	$('.showcls').tooltip({    
		position: 'bottom',
		trackMouse:'true',
		content: '<span>'+show+'</span>'    
	});
}
//销毁提示
function destoryToolTip(){
	$('.editcls').tooltip('destroy');
	$('.okcls').tooltip('destroy');
	$('.delcls').tooltip('destroy');
	$('.showcls').tooltip('destroy');
}
//为datagrid编辑状态的editors扩展textbox
$.extend($.fn.datagrid.defaults.editors, {
	textbox: {//为方法取名
		init: function (container, options) {
			var editor = $('<input />').appendTo(container);
			editor.textbox(options);
			return editor;
		},
		getValue: function (target) {//取值
		return $(target).textbox('getValue');
		},
		setValue: function (target, value) {//设置值
		$(target).textbox('setValue', value);
		},
		resize: function (target, width) {
		$(target).textbox('resize', width);
		},
		destroy: function (target) {
			$(target).textbox('destroy');//销毁生成的panel
		}
	}
});

//初始化datagrid
function initDataGrid(selector,url,paras,toolbar,id,cols,funSuccess){
	$(selector)
			.datagrid(
					{
						loadMsg : '数据加载中请稍后 ...',
						url : url,
						queryParams:paras,
						title:"",
						fitColumns : true,
						autoRowHeight : true,
						pagination : true,
						rownumbers : true,
						pageSize : 10,
						width : '100%',
						height : '100%',
						style:{
							margin:'0 auto'
						},
						toolbar : toolbar,
						pageList : [ 10, 20, 40, 80, 120 ],
						border : true,
						nowrap : true,
						striped : true,
						collapsible : true,
						singleSelect : true,
						ctrlSelect:true,
						idField : id,
						columns : cols,
						onLoadSuccess:funSuccess
					}).datagrid('getPager').pagination({
						pageNumber : 1,
						beforePageText : '第',
						afterPageText : '页    共 {pages} 页',
						displayMsg : '当前显示 {from} - {to} 条记录   共 {total} 条记录'
					});
}

function setFormCSS(){
//设置表单样式
	$(".cssform th").css({
		borderStyle:"solid",
		borderWidth:1,
		borderColor:$(".datagrid-toolbar").css("border-color"),
		background:$(".panel-header").css("background"),
		color:$(".panel-title").css("color"),
		fontWeight:'normal',
		fontSize:12,
		padding:'5px 0'
	});
	
	$(".cssform td").css({
		borderStyle:"solid",
		borderWidth:1,
		borderColor:$(".datagrid-toolbar").css("border-color"),
		background:$(".panel-body").css("background"),
		padding:5
	});
	
	$(".cssform input").css({
		background:'none',
		color:$(".panel-title").css("color"),
		width:'100%',
		border:0,
		fontSize:12
	});
}
/** @serializedParams looks like "prop1=value1&prop2=value2".
Nested property like 'prop.subprop=value' is also supported **/ 
function param2json (serializedParams) { 

var obj={}; 
function evalThem (str) { 
var attributeName = str.split("=")[0]; 
var attributeValue = str.split("=")[1]; 
if(!attributeValue){ 
return ; 
} 

var array = attributeName.split("."); 
for (var i = 1; i < array.length; i++) { 
var tmpArray = Array(); 
tmpArray.push("obj"); 
for (var j = 0; j < i; j++) { 
tmpArray.push(array[j]); 
}; 
var evalString = tmpArray.join("."); 
// alert(evalString); 
if(!eval(evalString)){ 
eval(evalString+"={};"); 
} 
}; 
eval("obj."+attributeName+"='"+attributeValue+"';"); 

}; 
var properties = serializedParams.split("&"); 
for (var i = 0; i < properties.length; i++) { 
evalThem(properties[i]); 
}; 
return obj; 
}
function showMsgBox(msg){
	if(!$("#MsgPopWin").html()){
		$('<div id="MsgPopWin" style="border:1px solid #ccc;position:absolute;z-index: 9999;padding: 5px;background-color: white;display:none">	<img  src="../img/yes-512x512.png" width="50" height="50" align="middle">	<span style="font:normal normal 16px 微软雅黑;position: relative;top: 5px;"></span></div>').appendTo($("body"));
	}
	$("#MsgPopWin span").text(msg);
	setCenter("#MsgPopWin");
		$("#MsgPopWin").delay(100).fadeIn("normal").delay(500).fadeOut("normal");
}

function setCenter(selector){
	var $e=$(selector);
	
	var page=getPageSize();
	var nDivLeft = page[2];
//alert(nDivLeft);
    // 整体宽度减去当前元素的宽度

          nDivLeft = nDivLeft - $e.outerWidth();

     // 计算中间位置

          nDivLeft = nDivLeft/2+$(document).scrollLeft();


          // 计算Top, 原理与上边的原理一致

          var nDivTop = page[3];
//alert(nDivTop);

          nDivTop = nDivTop - $e.outerHeight();

          nDivTop = nDivTop/2+$(document).scrollTop();

          $e.css({
          	top:nDivTop,
          	left:nDivLeft
          });
}
function getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else {
        if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac    
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari    
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer    
        if (document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else {
        if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode    
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        } else {
            if (document.body) { // other Explorers    
                windowWidth = document.body.clientWidth;
                windowHeight = document.body.clientHeight;
            }
        }
    }       
    // for small pages with total height less then height of the viewport    
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }    
    // for small pages with total width less then width of the viewport    
    if (xScroll < windowWidth) {
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
    return arrayPageSize;
}

var Common = {
    /**
     * 格式化日期（不含时间）
     * @param TimeStamp类型对象
     * @return 日期字符串XX年XX月XX日
     * @type String
     */
    formatDate: function (date) {
        if (date == undefined) {
            return "";
        } 
        var datetime =(date.year+1900) ;
        if(date.month<9){
       	 datetime+="-0"+(date.month+1);
        }else{
       	 datetime+="-"+(date.month+1);
        }
        if(date.date<10){
       	 datetime+="-0"+date.date+" ";
        }else{
       	 datetime+="-"+date.date+" ";
        }
        return datetime;
    },
    /**
     * 格式化日期（含时间"00:00:00"）
     * @param TimeStamp类型对象
     * @return 日期字符串XX年XX月XX日 XX:XX:XX
     * @type String
     */
    formatDateTime: function (date) {
    	 if (date == undefined) {
             return "";
         } 
         var datetime = (date.year+1900);
         if(date.month<9){
        	 datetime+="-0"+(date.month+1);
         }else{
        	 datetime+="-"+(date.month+1);
         }
         if(date.date<10){
        	 datetime+="-0"+date.date+" ";
         }else{
        	 datetime+="-"+date.date+" ";
         }
         var hours=date.hours>9?date.hours:"0"+date.hours;
         var minutes=date.minutes>9?date.minutes:"0"+date.minutes;
         var seconds=date.seconds>9?date.seconds:"0"+date.seconds;
         datetime=datetime+hours+":"+minutes+":"+seconds;
         return datetime;
    },
    /**  
     * 将数值四舍五入(保留2位小数)后格式化成金额形式  
     *  
     * @param num 数值(Number或者String)  
     * @return 金额格式的字符串,如'1,234,567.45'  
     * @type String  
     */    
    formatCurrency:function(num) {    
        num = num.toString().replace(/\$|\,/g,'');    
        if(isNaN(num))    
        num = "0";    
        sign = (num == (num = Math.abs(num)));    
        num = Math.floor(num*100+0.50000000001);    
        cents = num%100;    
        num = Math.floor(num/100).toString();    
        if(cents<10)    
        cents = "0" + cents;    
        for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)    
        num = num.substring(0,num.length-(4*i+3))+','+    
        num.substring(num.length-(4*i+3));    
        return (((sign)?'':'-') + num + '.' + cents);    
    } ,
    /**  
     * 将数值四舍五入(保留1位小数)后格式化成金额形式  
     *  
     * @param num 数值(Number或者String)  
     * @return 金额格式的字符串,如'1,234,567.4'  
     * @type String  
     */    
    formatCurrencyTenThou:function(num) {    
        num = num.toString().replace(/\$|\,/g,'');    
        if(isNaN(num))    
        num = "0";    
        sign = (num == (num = Math.abs(num)));    
        num = Math.floor(num*10+0.50000000001);    
        cents = num%10;    
        num = Math.floor(num/10).toString();    
        for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)    
        num = num.substring(0,num.length-(4*i+3))+','+    
        num.substring(num.length-(4*i+3));    
        return (((sign)?'':'-') + num + '.' + cents);    
    } ,
    /**  
     * 将数值四舍五入
     * @param num数值(Number或者String)  
     * @param precision保留几位小数。10表示保留1位小数，100表示保留2位小数,以此类推，如果小于等于1表示取整数部分。
     * @return 格式的数字字符串,如'1234.50'  
     * @type String
     */    
    formatDouble:function(num,precision) {    
        num = num.toString().replace(/\$|\,/g,'');    
        if(isNaN(num))    
        num = "0";    
        precision=Math.abs(precision);
        if(precision<1)
        	precision=1;
        if(precision==1){
        	return Math.floor(num*1+0.50000000001);
        }
        sign = (num == (num = Math.abs(num)));    
        num = Math.floor(num*precision+0.50000000001);    
        cents = num%precision;    
        num = Math.floor(num/precision).toString();    
        if(cents<10 && precision>10)    
        cents = "0" + cents;    
        return (((sign)?'':'-') + num + '.' + cents);    
    }
};